package com.salary.utils.reader;

import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class AttendanceDataReader extends DataReader<Map<String, Map<String, Double>>>{

    private static final Logger logger = LoggerFactory.getLogger(AttendanceDataReader.class);

    /**
     * 读取考勤表数据
     * @param attendanceFilePath Excel文件路径
     * @return 考勤数据，键为姓名，值为考勤信息映射
     */
    @Override
    public Map<String, Map<String, Double>> readData(String attendanceFilePath) {
        Map<String, Map<String, Double>> attendanceData = new HashMap<>();
        try (FileInputStream fis = new FileInputStream(attendanceFilePath);
             Workbook workbook = WorkbookFactory.create(fis)) {

            Sheet sheet = workbook.getSheetAt(0);
            if (sheet == null) {
                logger.error("未找到工作表");
                return attendanceData;
            }

            // 使用通用方法查找必要的列，更新为新表头
            Map<String, Integer> columnIndices = findColumnIndices(sheet,
                    new String[]{"姓名", "病假", "带薪假", "事假", "旷工", "迟到次数", "1.5倍", "2倍", "3倍", "实际出勤"});

            // 输出找到的列索引，便于调试
            for (Map.Entry<String, Integer> entry : columnIndices.entrySet()) {
                logger.info("考勤表中找到列: " + entry.getKey() + " 在第" + (entry.getValue()+1) + "列");
            }

            // 验证必要的列
            if (!columnIndices.containsKey("姓名")) {
                logger.error("考勤表中未找到姓名列");
                return attendanceData;
            }

            int nameCol = columnIndices.get("姓名");

            // 创建列名到类型的映射
            Map<Integer, String> columnTypes = new HashMap<>();
            for (Map.Entry<String, Integer> entry : columnIndices.entrySet()) {
                if (!entry.getKey().equals("姓名")) { // 排除姓名列
                    columnTypes.put(entry.getValue(), entry.getKey());
                }
            }

            // 从第二行开始读取数据（跳过表头）
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row == null) continue;

                Cell nameCell = row.getCell(nameCol);
                if (nameCell == null) continue;

                String name = getCellValueAsString(nameCell).trim();
                if (name.isEmpty()) continue;

                Map<String, Double> personData = new HashMap<>();

                // 读取所有考勤相关列的数据
                for (Map.Entry<Integer, String> entry : columnTypes.entrySet()) {
                    int colIndex = entry.getKey();
                    String colType = entry.getValue();

                    Cell cell = row.getCell(colIndex);
                    if (cell != null) {
                        double value = getCellValueAsDouble(cell);
                        if (value != 0) {
                            personData.put(colType, value);
                            logger.debug("读取到考勤数据: " + name + " - " + colType + ": " + value);
                        }
                    }
                }

                if (!personData.isEmpty()) {
                    attendanceData.put(name, personData);
                    logger.info("读取到" + name + "的考勤数据，共" + personData.size() + "项");
                }
            }

            logger.info("成功从考勤表读取了" + attendanceData.size() + "人的考勤数据");

        } catch (Exception e) {
            logger.error("读取考勤表数据时出错: " + e.getMessage(), e);
        }
        return attendanceData;
    }
}
